package Dao;

import entity.Decreate;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class DecreateDaoimpl implements DecreateDao{

    /*
    添加装扮相关内容
     */
    @Override
    public boolean insertDecreate(Decreate decreate) {
        try{
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\Decreate.txt",true));
            bufferedWriter.write(decreate.getId()+","+decreate.getBubble()+","+decreate.getTheme()+","+decreate.getBackground()+","+decreate.getPendant());
            bufferedWriter.newLine();
            bufferedWriter.close();
            return true;
        }
        catch(IOException e){
            System.out.println("IO异常,添加失败!");
            return false;
        }
    }
    /*
    根据用户id,查询装扮
     */
    @Override
    public List<Decreate> seekDecreateById(Integer id) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Decreate.txt"));
            List<Decreate> decreateList=new ArrayList<>();
            List<Decreate> decreateList1=new ArrayList<>();
            String string;
            while((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Decreate decreate1=new Decreate(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
                decreateList.add(decreate1);
            }
            for(int i=0;i< decreateList.size();i++){
                Decreate decreate1=decreateList.get(i);
                if(decreate1.getId().equals(id)){
                    decreateList1.add(decreate1);
                }
            }
            bufferedReader.close();
            return decreateList1;
        }
        catch(IOException e){
            System.out.println("io异常,查询失败!");
            return null;
        }
    }
    /*
    根据装扮主题删除装扮操作
     */
    @Override
    public boolean deleteDecreateByDecreateTheme(Integer id, String theme) {
     try{
         List<Decreate> decreateList1=seekDecreateById(id);
         BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Decreate.txt"));
         List<Decreate> decreateList=new ArrayList<>();
         String string;
         while((string=bufferedReader.readLine())!=null){
             String[] dates=string.split(",");
             Decreate decreate=new Decreate(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
             decreateList.add(decreate);
         }
         for(int i=0;i<decreateList.size();i++){
             Decreate decreate=decreateList.get(i);
             if(decreate.getId().equals(id)){
                 if(decreate.getTheme().equals(theme)){
                     decreateList.remove(i);
                 }
                 else{
                     System.out.println("用户里面不存在该装扮,删除失败!");
                 }
             }
             else{
                 System.out.println("查无此id,删除失败!");
             }
         }
         BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\Decreate.txt"));
         for(int i=0;i<decreateList.size();i++){
             Decreate decreate=decreateList.get(i);
             String returnString=String.format("%s,%s,%s,%s,%s",decreate.getId(),decreate.getBubble(),decreate.getTheme(),decreate.getBackground(),decreate.getPendant());
             bufferedWriter.write(returnString);
             bufferedWriter.newLine();
         }
         bufferedWriter.close();
         return true;
     }
     catch(IOException e){
         System.out.println("io异常,删除失败!");
         return false;
     }
    }
    /*
    更改用户装扮信息
     */
    public boolean changeDecreateById(Integer id,String theme,Decreate decreate){
        try{
           // List<Decreate> decreateList=decreateDao.seekDecreateById(id);
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Decreate.txt"));
            List<Decreate> decreateList=new ArrayList<>();
            String string;
            while((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Decreate decreate1=new Decreate(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
                decreateList.add(decreate1);
            }
            for(int i=0;i<decreateList.size();i++){
                Decreate decreate1=decreateList.get(i);
                if(decreate1.getId().equals(id)){
                    if (decreate1.getTheme().equals(theme)) {
                        decreateList.set(i,decreate);
                    }
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("QQSystem\\Decreate.txt"));
            for(int i=0;i<decreateList.size();i++){
                Decreate decreate1=decreateList.get(i);
                String returnString= String.format("%s,%s,%s,%s,%s",decreate1.getId(),decreate1.getBubble(),decreate1.getTheme(),decreate1.getBackground(),decreate1.getPendant());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            return true;
        }
        catch(IOException e){
            System.out.println("io异常,更改失败!");
            return false;
        }
    }
    /*
    查询所有用户装扮
     */
    @Override
    public List<Decreate> seekAllDecreate() {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("QQSystem\\Decreate.txt"));
            List<Decreate> decreateList=new ArrayList<>();
            String string;
            while((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                Decreate decreate=new Decreate(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4]);
                decreateList.add(decreate);
            }
            bufferedReader.close();
            return decreateList;
        }
        catch(IOException e){
            System.out.println("io异常,查询失败!");
            return null;
        }
    }
}
